/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.SysUserMapper;
import com.ktjiaoyu.service.SysUserService;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 科泰集团教研团队
 * @version 1.0.0
 * @packageName com.ktjiaoyu.service.impl
 * @fileName SysUserServiceImpl.java
 * @createTime 2024/10/1 22:06
 * @lastModify 2024/10/1 22:06
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SysUserServiceImpl.class);
    // @Resource
    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    @Transactional
    public int saveBatch(List<SysUser> sysUserList) {
        int rows = 0;
        for (int i = 0; i < sysUserList.size(); i++) {
            // 拿出每一条用户数据插入
            SysUser sysUser = sysUserList.get(i);
            if (i == 1) {
                // 插入第二条数据时，模拟数据库异常，查看事务是否生效
                throw new RuntimeException("数据库异常");
            }
            // 调用插入数据操作，计入总受影响行数
            rows += sysUserMapper.insert(sysUser);
        }
        // 返回总受影响行数
        return rows;
    }

    @Override
    public SysUser login(String account, String password) {
        SysUser loginUser = sysUserMapper.getByAccount(account);
        if (loginUser != null && loginUser.getPassword().equals(password)) {
            return loginUser;
        }
        return null;
    }

    /**
     * 多条件查询用户分页数据
     */
    @Override
    public Page<SysUser> findPage(SysUser sysUser, Integer pageNo, Integer pageSize) throws Exception {
        Page<SysUser> page = null;
        try {
            page = new Page<>();
            page.setPageNo(pageNo);
            page.setPageSize(pageSize);
            //计算总页数
            Long count = sysUserMapper.count(sysUser);
            page.setTotalCount(count.intValue());
            //查询分页数据
            List<SysUser> data = sysUserMapper.getPage(sysUser, page.offset(), page.getPageSize());
            page.setData(data);
        } catch (Exception e) {
            LOGGER.error("执行SysUserServiceImpl.findPage(sysUser,pageNo, pageSize)方法出现异常", e);
            throw e;
        }
        return page;
    }

    @Override
    public boolean saveOrUpdate(SysUser sysUser) throws Exception {
        try {
            if (sysUser.getId() == null) {
                return sysUserMapper.insert(sysUser) > 0;
            } else {
                return sysUserMapper.update(sysUser) > 0;
            }
        } catch (Exception e) {
            LOGGER.error("执行SysUserServiceImpl.saveOrUpdate(sysUser)方法出现异常", e);
            throw e;
        }
    }

    @Override
    public SysUser findById(Long id) throws Exception {
        try {
            return sysUserMapper.findById(id);
        } catch (Exception e) {
            LOGGER.error("执行SysUserServiceImpl.findById(id)方法出现异常", e);
            throw e;
        }
    }

    @Override
    public SysUser findByAccount(String account) throws Exception {
        try {
            return sysUserMapper.getByAccount(account);
        } catch (Exception e) {
            LOGGER.error("执行SysUserServiceImpl.findByAccount(account)方法出现异常", e);
            throw e;
        }
    }

    @Override
    public boolean deleteById(Long id) throws Exception {
        return sysUserMapper.deleteById(id) > 0;
    }

    // 提供setter方法用于注入属性
    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }
}
